package executors;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import objects.Command;
import objects.Event;
import objects.Result;
import objects.SortedEventList;
import test.DummyEventGenerator;

public class WriteExecutor {
	private final static boolean FAIL = false;
	private final static boolean SUCCESSFUL = true;

	public static Result execute(Command command, SortedEventList openTasks,
			SortedEventList completeTasks) {
		// TODO Auto-generated method stub
		ArrayList<Event> openTask = openTasks.getArrayList();
		ArrayList<Event> completeTask = completeTasks.getArrayList();
		IOException e = writeToFile(openTask, completeTask);
		if (e == null) {
			return new Result(SUCCESSFUL, null, null, null);
		}
		return new Result(FAIL, null, null, e.toString());
	}

	/** This method writes content from text to file */
	private static IOException writeToFile(ArrayList<Event> openTasks,
			ArrayList<Event> completeTasks) {
		BufferedWriter outer;
		try {
			outer = new BufferedWriter(new FileWriter("GeekDo.txt"));
			outer.write("Open Tasks");
			outer.newLine();
			for (int i = 0; i < openTasks.size(); i++) {
				outer.write(i + 1 + ". ");
				Event e = openTasks.get(i);
				String s = e.toString();
				outer.write(s);
				outer.newLine();
			}
			outer.write("Complete Tasks");
			outer.newLine();
			for (int i = 0; i < completeTasks.size(); i++) {
				outer.write(i + 1 + ". ");
				Event e = completeTasks.get(i);
				String s = e.toString();
				outer.write(s);
				outer.newLine();
			}
			outer.close();
		} catch (IOException e) {
			return e;
		}
		return null;
	}

	

	public static void main(String args[]) {
		int testSize = 10;
		ArrayList<Event> eventList1 = new ArrayList<Event>();
		for (int i = 0; i < testSize; i++) {
			eventList1.add(DummyEventGenerator.getDummyEvent());
		}
		ArrayList<Event> subtasks = new ArrayList<Event>();
		for (int i = 0; i < testSize; i++) {
			subtasks.add(DummyEventGenerator.getDummyEvent());
		}
		eventList1.get(0).setSubTask(subtasks);
		eventList1.get(1).setSubTask(subtasks);
		ArrayList<Event> eventList2 = new ArrayList<Event>();
		
		for (int i = 0; i < testSize; i++) {
			eventList2.add(DummyEventGenerator.getDummyEvent());
		}
		writeToFile(eventList1, eventList2);
	}
}